using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using AjGenesisStudio.Properties;
using System.IO;

namespace AjGenesisStudio.Commands
{
    class AddExistingCommand : Command
    {
        string filepath;
        string destFolder;

        public AddExistingCommand() { }
        public AddExistingCommand(string destFolder)
        {
            this.destFolder = destFolder;
        }

        public string DestFolder
        {
            get { return destFolder; }
            set { destFolder = value; }
        }
	

        public override bool Execute()
        {
            if (ShowOpen())
                AddFile();
            return true;
        }

        private bool ShowOpen()
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "All files (*.*)|*.*|Template file (*.tpl)|*.tpl|XML document (*.xml)|*.xml|AjGenesis task file (*.ajg)|*.ajg|NAnt build file (*.build)|*.build";
            dlg.CheckFileExists = true;
            dlg.CheckPathExists = true;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                filepath = dlg.FileName;
                return true;
            }
            else
            {
                return false;
            }
        }

        private void AddFile()
        {
            Global.Instance.StartWork("Adding " + Path.GetFileName(filepath));

            if (string.IsNullOrEmpty(destFolder))
                destFolder = Panels.ExplorerPanel.Instance.SelectedPath;
            if (string.IsNullOrEmpty(destFolder))
                return;
            if (!Directory.Exists(destFolder)) //no es un directorio
                destFolder = Path.GetDirectoryName(destFolder);

            if (Directory.Exists(destFolder))
                Utils.ShellHelper.CopyFiles(filepath, destFolder);

            Global.Instance.EndWork();
        }
    }
}
